﻿using AutoMapper;
using D.Medicalcare.Domain.Entities;
using D.Medicalcare.ErrorCode;
using D.Medicalcare.Infrastructure.Interfaces;
using D.Medicalcare.Write.API.Application.Commands.RoleCommand;
using MediatR;

namespace D.Medicalcare.Write.API.Application.Handlers.RoleHandler
{
    public class UpdateRoleHandler : IRequestHandler<UpdateRoleCommand, APIResult<object>>
    {

        private readonly IBaseRepository<Role> roleRep;
        private readonly IMapper mapper;

        public UpdateRoleHandler(IBaseRepository<Role> roleRep, IMapper mapper)
        {
            this.roleRep = roleRep;
            this.mapper = mapper;
        }

        public Task<APIResult<object>> Handle(UpdateRoleCommand request, CancellationToken cancellationToken)
        {
            APIResult<object> result = new APIResult<object>();
            try
            {
                 var model=mapper.Map<Role>(request);
                model.UpdateTime= DateTime.Now; 
                model.RoleName= request.RoleName;
                model.RoleDesc= request.RoleDesc;
                model.IsDeleted= request.IsDeleted;
                model.UpdateUserId= request.UpdateUserId;
                var s = roleRep.Update(model).Result;
                if (s > 0)
                {
                    result.Code = APIEnum.Success;
                    result.Msg = "修改成功";
                    result.Data = s;
                }
                else
                {
                    result.Code = APIEnum.Failed;
                    result.Msg = "修改失败";
                    result.Data = s;
                }
                return Task.FromResult(result);
            }
            catch (Exception)
            {

                throw;
            }
        }
    }
}
